﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Globalization;
using System.Data;
using System.Data.Entity;

namespace System
{
    public static class EnumExtensionMethods
    {
        
        /*
        private static TResult ConvertToEnum<TInput, TResult>(this int source)
        {
            if (!typeof(TResult).IsEnum)
            {
                throw new NotSupportedException("TResult must be an Enum");
            }
            return (TResult)Enum.Parse(typeof(TResult), source.ToString());
        }


        
        /// <summary>
        /// This method attempts to convert the Enum Value to the given Enum type and value. Basically saves specifically casting and geting
        /// the type
        /// </summary>
        /// <typeparam name="T">This is the type of enum to convert to</typeparam>
        /// <param name="pEnum">This is the object being extended</param>
        /// <param name="pEnumValueToConvert">The enum to convert to</param>
        /// <returns>An enum of the type T if a match can be found</returns>
        public static T ConvertToEnum<T>(this Enum pEnum, T pEnumValueToConvertToType)
        {
            try
            {
                return (T)Enum.Parse(typeof(T), pEnum.ToString());
            }
            catch (Exception)
            {
                throw new FormatException("Unable to Convert Enum values, most likely cause is value(" + pEnum.ToString() + ") is missing from the target enum");
            }
        }//end extension method
         */

        public static string RetornarDiaPorExtenso(this int diaDaSemana)
        {
            return CultureInfo.CurrentCulture.DateTimeFormat.DayNames[(int)diaDaSemana];
        }
        public static string RetornarDiaPorExtenso(this DayOfWeek diaDaSemana)
        {
            return ((int)diaDaSemana).RetornarDiaPorExtenso();
        }

        public static T EnumTypeTo<T>(this object value)
        {
            Type conversionType = typeof(T);

            if (!conversionType.IsGenericType && conversionType.IsEnum)
            {
                try
                {
                    if (value.GetType().IsValueType)
                    {
                        return (T)Enum.ToObject(conversionType, value);
                    }
                    else
                    {
                        return (T)Enum.Parse(conversionType, value.ToString());
                    }
                }
                catch
                {
                    return default(T);
                }
            }

            try
            {
                return (T)Convert.ChangeType(value, conversionType);
            }
            catch
            {
                return default(T);
            }
        }
    }
}
